package com.gopay.bis.service.bank.impl.ebank;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.ebank.CmbcB2bManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.CmbcConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmbc.ebank.CmbcB2bMessage;
import com.gopay.common.domain.bank.cmbc.ebank.CmbcMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankInteractRemoteService;

public class CmbcB2bBankServiceImpl implements BankInteractRemoteService {
	
	@Resource(name = "cmbcB2bManager")
	private CmbcB2bManager cmbcB2bManager;

	@Override
	public Result pay(BaseMessage base) {
		Result result = new Result(false);
		try {
			CmbcMessage messageB2B = new CmbcMessage();
			BeanUtils.copyProperties(base, messageB2B);
			messageB2B = (CmbcMessage) cmbcB2bManager.sign(messageB2B);
			result.setDefaultModel(messageB2B);
			result.setSuccess(true);
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6028);
		}
		return result;
	}

	@Override
	public Result bankBack(BaseMessage base) {
		Result result = new Result(false);
		try {
			boolean flag = false;
			CmbcB2bMessage messageB2B = new CmbcB2bMessage();
			BeanUtils.copyProperties(base, messageB2B);
			HashMap map = (HashMap) cmbcB2bManager.verify(messageB2B);
			messageB2B = (CmbcB2bMessage)map.get("cmbcMessage");
			flag = (Boolean)map.get("flag");
			result.setDefaultModel(flag);
			result.setSuccess(true);
			result.setModel("res", messageB2B);

			Map<String, String> param = new HashMap<String, String>();
			// 通知后台地址入账
			if (flag) {
				if (messageB2B.getOrderStatus().equals(CmbcConstants.ORDER_STATE_SUCCESS)) {										
					param.put("orderId", messageB2B.getOrderId());
					param.put("amt", messageB2B.getAmount().toString());
					param.put("cardType", "Unknown");
					param.put("notifyNum", "1");
					param.put("payStatus", "00");
					param.put("retCode", "00");
					param.put("retMsg", "验签成功");
					param.put("signData", messageB2B.getSignMessage());
					param.put("isFront", String.valueOf(base.isFront()));
					result.setModel("isNotifyCashier", param);
				} else {
					// 通知后台地址该订单失败
					param.put("orderId", messageB2B.getOrderId());
					param.put("amt", messageB2B.getAmount().toString());
					param.put("cardType", "Unknown");
					param.put("notifyNum", "1");
					param.put("payStatus", "01");
					param.put("retCode", "01");
					param.put("retMsg", messageB2B.getResMessage());
					param.put("signData", messageB2B.getSignMessage());
					param.put("isFront", String.valueOf(base.isFront()));
					result.setModel("isNotifyCashier", param);
				}
			}
		
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
		}
		return result;
	}

	@Override
	public Result query(BaseMessage base) {
		Result result = new Result(false);
		try {
			CmbcMessage messageB2B = (CmbcMessage)base;
			BeanUtils.copyProperties(base, messageB2B);
			CmbcMessage res = (CmbcMessage) cmbcB2bManager.query(messageB2B);
			result.setDefaultModel(res);
			result.setSuccess(true);
			if(BisPayCheckUtils.checkExternalOrderID(messageB2B.getOrderId())){
				Map<String, String> param = new HashMap<String, String>();
				param.put("orderId", messageB2B.getOrderId());
				param.put("amount", messageB2B.getAmount().toString());
				if (res == null) {
					// 查询订单为空
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", "银行返回对账结果为空");
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					result.setModel("queryResult", param);
					return result;
				}
				if (!CmbcConstants.RES_CODE_SUCCESS.equalsIgnoreCase(res.getResCode())) { // 如果查询本身失败，跳过这一笔
					// 查询订单错误
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", res.getResMessage());
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
				}else{
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);
					// 如果查询成功，则一定有定单信息
					if (!(CmbcConstants.ORDER_STATE_SUCCESS.equalsIgnoreCase(res.getOrderStatus()) || CmbcConstants.ORDER_STATE_FAILE.equalsIgnoreCase(res.getOrderStatus()))) { // 如果状态未明
						param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
						param.put("returnMessage", "银行返回交易未明确");
					} else {
						// 状态明确，成功或失败
						if (CmbcConstants.ORDER_STATE_SUCCESS.equalsIgnoreCase(res.getOrderStatus())) {// 如果成功							
							param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
							param.put("returnMessage", "交易成功");
						} else {// 如果失败
							param.put("tradeState", BisPayConstants.TRADESTATE_FAIL);
							param.put("returnMessage", "交易失败");
						}
					}
				}
				result.setModel("queryResult", param);
			}
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
			result.setResultCode(e.getErrCode());
		} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6032);
		}
		return result;
	}

	@Override
	public Result refund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result dayend(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result queryBack(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

    @Override
    public Result refundBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundQuery(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

	@Override
	public Result sendBankRefund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

}
